package com.udf.service.impl;

import com.udf.bean.MatchResultBean;
import com.udf.service.IContentMatchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 张未然 on 2015/9/2.
 */
@Service
public class ContentMatchService implements IContentMatchService {

    private static Logger log = LoggerFactory.getLogger(ContentMatchService.class);


    /**
     * 以正则表达式检验content中含不含有keywords
     * 效率比字符串匹配操作好，而且keyword支持正则表达式
     * @param content
     * @param keywords
     * @return
     */
    @Override
    public MatchResultBean match(String content,List<String> keywords) {
        boolean success = true;
        ArrayList<String> unHitKeyWords = new ArrayList<>();
        if(keywords!=null&&!keywords.isEmpty()){
            for (String keyword : keywords) {
                if(!StringUtils.isEmpty(keyword)){
                    Pattern fixPattern = Pattern.compile(keyword);
                    Matcher matcher = fixPattern.matcher(content);
                    if(!matcher.find()){
                        success = false;
                        unHitKeyWords.add(keyword);
                    }
                }
            }
        }
        return new MatchResultBean(success,unHitKeyWords);
    }
}
